import qiniuUploader from './qiniuUploader.js';



export const ImgUploads = (token, domain, address, chooseImageRes, group_nums) => {
	let file = chooseImageRes
	return new Promise((resolve, reject) => {
		if (file.length > 1) {

		} else {
			file.forEach(itme => {
				if (group_nums == 'user') {
					var key = address + uni.getStorageSync('uid') + "_" + group_nums + '_' + Date
						.parse(new Date()) + ".png";
				} else {
					var key = address + uni.getStorageSync('uid') + "_" + group_nums + "_" + file
						.name;
				}
				qiniuUploader.upload(itme.path, (res) => {
					this.setData({
						'imageURL': res.imageURL,
					});
				}, (error) => {
					console.log('error: ' + error);
				}, {
					region: 'SCN', //服务器区域号
					domain: domain, //七牛云域名
					key: key, // 上传到七牛云图片就是这路径
					uptoken: token, // 由其他程序生成七牛 uptoken 
				}, (res) => {
					console.log('上传进度', res.progress)
					console.log('已经上传的数据长度', res.totalBytesSent)
					console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
					console.log(res)
				}, (res) => {
					// 取消上传
				}, (res) => {
					// `before` 上传前执行的操作
				}, (res) => {
					console.log('res', res)
					// `complete` 上传接受后执行的操作(无论成功还是失败都执行)
				});
			})
		}
	})


}


















export const imgUploads = (token, domain, address, file, group_nums) => {
	return new Promise((resolve, reject) => {
		if (group_nums == 'user') {
			var key = address + uni.getStorageSync('uid') + "_" + group_nums + '_' + Date.parse(
				new Date()) + ".png";
		} else {
			var key = address + uni.getStorageSync('uid') + "_" + group_nums + "_" + file.name;
		}
		var config = {
			region: 'SCN',
			uptoken: token,
			domain: domain, //配置好的七牛云域名  如   https://cdn.qniyun.com/
			unique_names: false,
		};
		let putExtra = {
			fname: "",
			params: {},
			mimeType: [] || null
		};
		let observable = qiniuUploader.upload(file, key, token, putExtra, config)
		let subscription = observable.subscribe({
			next(res) {
				if (res.total.percent === 100) {
					resolve(key)
				}
			}, // 接收上传进度信息的回调函数
			error(err) {
				reject('上传失败');
			}, //上传错误后触发
			complete(res) {} //停止当前文件上传
		}) // 上传开始

	})


}
//批量
export const imgUploads2 = async (token, domain, address, file) => {
	let arr = [];
	let file2 = Object.keys(file);
	for (let i = 0; i < file2.length; i++) {
		arr.push(await imgUploads(token, domain, address, file[i].file));
	}
	return arr;
};